Page({
  data: {
    bazi: '',
    wuxing: []
  },
  onLoad: function(options) {
    try {
      const apiData = JSON.parse(decodeURIComponent(options.data));
      this.setData({
        basicInfo: Object.entries(apiData.基本信息).map(([key, value]) => ({ key, value })),
        paipan: apiData.基本排盘,
        wuxing: this.formatWuxingData(apiData.五行)
      });
      wx.hideLoading();
    } catch (e) {
      wx.hideLoading();
      wx.showToast({title: '数据解析失败', icon: 'none'});
      wx.navigateBack();
    }
  },
  formatWuxingData: function(wuxingData) {
    const totalMain = Object.values(wuxingData.五行个数).reduce((sum, cur) => sum + parseInt(cur), 0);
    const totalCangzang = Object.values(wuxingData.含藏干数).reduce((sum, cur) => sum + parseInt(cur), 0);
    const colorMap = {
      '木': '#4CAF50',
      '火': '#F44336',
      '土': '#8B4513',
      '金': '#FFC107',
      '水': '#2196F3'
    };
    return ['木','火','土','金','水'].map(name => ({
      name,
      value: wuxingData.五行个数[name],
      cangzang: wuxingData.含藏干数[name],
      percent: (parseInt(wuxingData.五行个数[name])/totalMain*100).toFixed(0),
      cangzangPercent: (parseInt(wuxingData.含藏干数[name])/totalCangzang*100).toFixed(0),
      color: colorMap[name]
    }));
  }
})